﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using System.Data.Entity; // needed for lambda includes


namespace TheCup.ApiControllers
{

    public class CommentController : ApiController
    {
        TheCupContext ctx = new TheCupContext();

        private const int COMMENT_TAKE = 12;

        /// <summary>
        /// Get 10 latest comments - made for the front page
        /// </summary>
        /// <returns>IEnumerable</returns>
        public IEnumerable<CommentViewModel> Get()
        {
            DateTime margin = DateTime.Now.AddDays(-14);
            var r1 = (from c in ctx.Comments
                      join b in ctx.Blogs on c.LinkUuid equals b.OldId
                      where c.DateEntry > margin
                      orderby c.DateEntry descending
                      select new CommentViewModel { Id = b.Id, DateEntry = c.DateEntry, Text = c.Text, Player = c.Player, Name = b.Title, CommentType = CommentEntity.Blog }).Take(COMMENT_TAKE).ToList<CommentViewModel>();

            var r2 = (from c in ctx.Comments
                      join e in ctx.Events on c.LinkUuid equals e.uuid
                      where c.DateEntry > margin
                      orderby c.DateEntry descending
                      select new CommentViewModel { Id = e.Id, DateEntry = c.DateEntry, Text = c.Text, Player = c.Player, Name = e.Name, CommentType = CommentEntity.Event }).Take(COMMENT_TAKE).ToList<CommentViewModel>();

            var r3 = (from c in ctx.Comments
                      join r in ctx.Rounds on c.LinkUuid equals r.uuid
                      where c.DateEntry > margin && !r.IsPrivate
                      orderby c.DateEntry descending
                      select new CommentViewModel { Id = r.Id, DateEntry = c.DateEntry, Text = c.Text, Player = c.Player, Name = r.Course.Name, CommentType = CommentEntity.Round }).Take(COMMENT_TAKE).ToList<CommentViewModel>();

            List<CommentViewModel> test = r1.Union(r2).Union(r3).OrderByDescending(r => r.DateEntry).Take(COMMENT_TAKE).ToList<CommentViewModel>();

            return test;
        }

        // GET api/<controller>
        public IEnumerable<Comment> Get(Guid linkId)
        {
            List<Comment> e = new List<Comment>();
            e = (from i in ctx.Comments.Include(ed => ed.Player) select i)
                 .OrderByDescending(d => d.DateEntry)
                 .Where(c => c.LinkUuid == linkId)
                 .ToList<Comment>();
            return e;
        }

        // GET api/<controller>/5
        public IHttpActionResult Details(int id)
        {
            Comment e = new Comment();

            e = ctx.Comments.FirstOrDefault(c => c.Id == id);
            if (e == null)
            {
                return NotFound();
            }
            return Ok(e);
        }

        [HttpPut]
        [Route("Contact/{contactId:int}/name", Name = "UpdateContactName")]
        [ResponseType(typeof(void))] // <- what should I put here? do I even need it at all?
        public IHttpActionResult DummyForReference(int contactId, [FromBody] string name)
        {
            //...
            return StatusCode(HttpStatusCode.Accepted);
        }

        // POST api/<controller>
        [Authorize]
        public void Post([FromBody]string value)
        {
            Comment c = new Comment { Text = value };

            ctx.Comments.Add(c);
            //ctx.SaveChanges();
        }

        // PUT api/<controller>/5
        [Authorize]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        [Authorize]
        public void Delete(int id)
        {
            Comment comment = (from c in ctx.Comments
                         where c.Id == id
                         select c).FirstOrDefault();
            Player p = new PlayerController().GetLoggedInPlayer();
            if (comment.PlayerId == p.Id) { 
                ctx.Comments.Remove(comment);
                ctx.SaveChanges();
            }
        }

    }

}